# import urllib.request
# weburl="http://www.edu2act.cn"
# webheader1={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586 '}#Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586
# webheader2={
#     'Connection':'Keep-Alive',
#     'Accept':'text/html,application/xhtml+xml,*/*',
#     'Accept-Language':'en-US,en;q=0.8',
#     'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586',
#     'Host':'www.edu2act.cn',
#     'DNT':'1'
# }
# re=urllib.request.Request(url=weburl,headers=webheader2)
# webPage=urllib.request.urlopen(re)
# data=webPage.read()
# data=data.decode('UTF-8')
# print(data)
# print(type(webPage))
# print(webPage.geturl())
# print(webPage.info())
# print(webPage.getcode())

import urllib.request as ure
import socket
import re as reeeeee
import sys
import os
Dir=r'E:\开发区\Python\untitled\123'
def destFile(path):
    if not os.path.isdir(Dir):
        os.mkdir(Dir)
    pos=path.rindex('/')
    r=os.path.join(Dir,path[pos+1:])
    return r

if __name__=="__main__":
    weburl = "http://www.edu2act.cn"
    webheader1={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586 '}#Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586
    webheader2={
        'Connection':'Keep-Alive',
        'Accept':'text/html,application/xhtml+xml,*/*',
        'Accept-Language':'en-US,en;q=0.8',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586',
        'Host':'www.edu2act.cn',
        'DNT':'1'
    }
    re=ure.Request(url=weburl,headers=webheader2)
    webPage=ure.urlopen(re)
    data=webPage.read()
    data=data.decode('UTF-8')
    zz=reeeeee.compile("(/static[^\s]*?(jpg|png|gif))|(http:[^\s]*?(jpg|png|gif))")
    b=0
    for link,a,httplink,c in zz.findall(str(data)):
    #for link,a in set(reeee.findall("(/static[^\s]*?(jpg|png|gif))|(http:[^\s]*?(jpg|png|gif))",str(data))):
        if 'http' in httplink:
            b+=1
            print(httplink)
            try:
                ure.urlretrieve(httplink, destFile(httplink))
            except:
                print("异常")
        else:
            b+=1
            link='http://www.edu2act.cn'+link
            print(link)
            try:
                ure.urlretrieve(link, destFile(link))
            except:
                print("异常")
    print("共有"+str(b)+"张图片")
